import json as json_parser
import requests

from api_setting import *
from setting import *


class RestClient:
    def __init__(self):
        self.session = requests.session()
        self.api_root_url = IP_SIT
        self.username = USERNAME
        self.password = PASSWORD
        self.session.headers["Token"] = self.get_token()

    def request(self, url, method_name, data=None, json=None, **kwargs):
        url = self.api_root_url + url
        if method_name == "get":
            return process(self.session.get(url, **kwargs))

        if method_name == "post":
            return process(self.session.post(url, data, json, **kwargs))

        if method_name == "options":
            return process(self.session.options(url, **kwargs))

        if method_name == "head":
            return process(self.session.head(url, **kwargs))

        if method_name == "put":
            return process(self.session.put(url, data, **kwargs))

        if method_name == "patch":
            if json:
                data = json_parser.dumps(json)
            return process(self.session.patch(url, data, **kwargs))

        if method_name == "delete":
            return process(self.session.delete(url, **kwargs))

    def get(self, url, **kwargs):
        return self.request(url, "get", **kwargs)

    def post(self, url, data=None, json=None, **kwargs):
        return self.request(url, "post", data, json, **kwargs)

    def options(self, url, **kwargs):
        return self.request(url, "options", **kwargs)

    def head(self, url, **kwargs):
        return self.request(url, "head", **kwargs)

    def put(self, url, data=None, **kwargs):
        return self.request(url, "put", data, **kwargs)

    def patch(self, url, data=None, **kwargs):
        return self.request(url, "patch", data, **kwargs)

    def delete(self, url, **kwargs):
        return self.request(url, "delete", **kwargs)

    def get_token(self, username=None, password=None):
        """
        获取用户登录 token
        :param username: 用户名
        :param password: 密码
        :return: token
        """
        if username is None:
            username = self.username
        if password is None:
            password = self.password

        login_url = LOGIN_URL

        login_params = {
            "username": username,
            "password": password,
            "menuTypeAdmin": "CoreBackend",
        }

        r = self.post(url=login_url, params=login_params)

        # return json.dumps(r.json(), indent=2, ensure_ascii=False)
        # return r.json()["data"]["loginUser"]["token"]
        return r.content["data"]["loginUser"]["token"]


class Response:
    def __init__(self):
        self.status = None
        self.content = None
        self.raw = None
        self.text = None


def process(raw_response):
    response = Response()
    response.raw = raw_response
    response.status_code = raw_response.status_code
    response.text = raw_response.text

    try:
        response.content = raw_response.json()
    except:
        response.content = raw_response.content

    return response


if __name__ == "__main__":
    test = RestClient()
    print(test.get_token())
